package activity.login;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import presenter.UserPresenter;
import util.utls.ByAlert;
import util.utls.IsUtilUid;
import util.utls.Timer;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2016/12/5.
 */

public class ActivityVerficationCode extends BaseActivity implements Observer{
   private TextView btnVerficationCode;
    private ImageView back;
    private TextView tvTitle;
    private TextView tvTest;

    TextView pass_two_phone;
    EditText pass_code;
    TextView pass_two_ok;
    String phone;

    UserPresenter userPresenter;
    Timer timer;

//   private MySmsObserver mySmsObserver;
    public static final int MSG_RECEIVER_CODE = 1;
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case MSG_RECEIVER_CODE:
//                    String message = (String) msg.obj;
//                    pass_code.setText(message);
//                    pass_code.setSelection(message.length());
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_verification_code);
        tvTitle.setText("填写验证码");
        tvTest.setVisibility(View.GONE);

        timer = new Timer(60000, 1000, btnVerficationCode);// 构造CountDownTimer对象
        timer.start();

        userPresenter = new UserPresenter(this);

        phone = getIntent().getStringExtra("phone");
        pass_two_phone.setText(phone);


//        mySmsObserver = new MySmsObserver(this, handler);
//        Uri uri = Uri.parse("content://sms");
//        getContentResolver().registerContentObserver(uri, true, mySmsObserver);


    }
        @Override
        protected void onDestroy() {
            super.onDestroy();
 //        getContentResolver().unregisterContentObserver(mySmsObserver);
        }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btnVerficationCode:
//                Intent Intent = new Intent(MyApplcation.ctx, ActivityRestPasswoldSIn.class);
//                startActivity(Intent);
//                break;
            case R.id.back:
                this.finish();
                break;
            case R.id.pass_two_nel:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"400-863-9156"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.pass_two_ok:
                showProgressDialog("   数据加载中...",true);
                userPresenter.YanZhengverificationCode("+86",phone,pass_code.getText().toString(),2+"");

                break;
            case R.id.btnVerficationCode:
                showProgressDialog("   数据加载中...",true);
                userPresenter.sendVerificationCode("+86",phone,2+"");

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.send_code_success){
           //     ByAlert.alert(entity.getData());
                timer = new Timer(60000, 1000, btnVerficationCode);// 构造CountDownTimer对象
                timer.start();

            }
            if(entity.getEventType()==UserPresenter.send_code_fail){
             //   ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_success){
                ByAlert.alert(entity.getData());
                Intent intent=new Intent(this,ActivityRestPasswoldSIn.class);
                intent.putExtra("phone", phone);
                intent.putExtra("code", pass_code.getText().toString());
                startActivity(intent);
                this.finish();
                try {
                    closeProgressDialog();
                }catch (Exception Exception){}
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        }catch (Exception Exception){

        }
    }


}
